/**
 * 
 */
package cc.telecomdigital.tdstock.activity.groups.more.push.activity;

import cc.telecomdigital.tdstock.ProgressDialog;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.NetworkChangeListener.CONNECT_TYPE;
import cc.telecomdigital.tdstock.Framework.UniqueItem.NetworkChangeListener.INetworkChangeListener;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.more.push.client.Constants;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.Data;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.MessageClickAsyncTask;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.MessagePushAsyncTask;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.WebViewCopy;
import android.content.Intent;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

/**  
 *   
 * Project：push
 * Package：cc.telecomdigital.tdstock.activity.groups.more.push.activity
 * Class：NewsContent
 * Description：  
 * Creater：hunter
 * CreateTime：Oct 10, 2011 5:06:54 PM 
 * Modifier ：hunter
 * Modify time：Oct 10, 2011 5:06:54 PM 
 * Modify Remarks：  
 * @version   
 *   
 */
public class MessageContentActivity extends BaseActivity implements
	INetworkChangeListener
{

	protected static final String	TAG			= MessageContentActivity.class
												.getSimpleName();
	private WebView			_webView;
	private Button				_toolBtnReturn;
	private String				_message_id	= "";
	private String				_message_url	= "";

	private boolean			isFinished;

	public static int			type			= 0;					//1--push,2---cilck

	private WebViewCopy			copy;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.stock_message_content);
		_toolBtnReturn = (Button) findViewById(R.id.more_r);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				/**/
				Intent intent = new Intent(getApplicationContext(),
						MessageHistoryListActivity.class);
				if (MessageContentActivity.this.getIntent() != null
						&& MessageHistoryListActivity.class
								.getSimpleName()
								.equals(
										MessageContentActivity.this
												.getIntent()
												.getStringExtra(
														Constants.INTENT_KEY_COME_FROM)))
				{
					intent
							.putExtra(
									Constants.INTENT_KEY_COME_FROM,
									MessageHistoryListActivity.COME_FROM_ENTRY_CONTENT);
				}
				else
				{
					intent.putExtra(Constants.INTENT_KEY_COME_FROM, "");
				}

				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
						| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(MessageHistoryListActivity.class,
						intent);
				//
			}

		});

		_message_id = this.getIntent().getStringExtra(
				Constants.INTENT_KEY_DETAIL_MESSAGE_ID);
		_message_url = this.getIntent().getStringExtra(
				Constants.INTENT_KEY_DETAIL_MESSAGE_URL);
		/*ViewGroup viewGroup=(ViewGroup)findViewById(R.id.web_content);
		_webView =new WebView(this.getParent()==null?this:this.getParent());
		LinearLayout.LayoutParams params=new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
		
		viewGroup.addView(_webView,params); */

		_webView = (WebView) (findViewById(R.id.message_content_webview));
		_webView.getSettings().setBuiltInZoomControls(true);
		_webView.getSettings().setJavaScriptEnabled(true);

		copy = new WebViewCopy(this, _webView);
	}

	@Override
	protected void onResume()
	{

		TDStockLog.d(TAG, "MessageId=" + _message_id);
		super.onResume();
		postDelayed();
	}

	/* (non-Javadoc)
	 * @see cc.telecomdigital.tdstock.Framework.ActivityGroup_Framework#GetActivityGroupController()
	 */
	@Override
	protected ActivityGroupController GetActivityGroupController()
	{
		// TODO Auto-generated method stub
		return MoreGroup.GetActivityGroupHostControl();
	}

	//According to url load web html
	public void setWebViewContent(final String url)
	{
		if (_webView != null)
		{
			_webView.post(new Runnable()
			{

				@Override
				public void run()
				{
					// TODO Auto-generated method stub
					_webView.loadUrl(url);
					isFinished = true;
				}
			});
		}
	}

	private void postDelayed()
	{
		if (type == 0)
		{
			//setWebContextDefault();
			return;
		}//push
		else if (type == 1)
		{
			pushMessage(Data.message_type_id, _message_id);
		}//click
		else if (type == 2)
		{
			clickMessage(_message_url);
		}
		else
		{

		}
		type = 0;
	}

	//According to id load message html
	public void pushMessage(String messagetypeid, String id)
	{
		if (null == id || "".equals(id))
		{
			return;
		}
		if (null == Data.message_type_id || "".equals(messagetypeid))
		{
			Data.message_type_id = "2";
		}
		new MessagePushAsyncTask(MessageContentActivity.this.getParent(),
				Data.message_type_id, id)
		{
			String	_temp_url	= null;

			@Override
			protected Void doInBackground(String... params)
			{
				super.doInBackground(params);
				// TODO Auto-generated method stub
				_temp_url = this.getUrl();

				return null;
			}

			@Override
			protected void onPostExecute(Void result)
			{
				super.onPostExecute(result);
				/*if(!result.equals("")){
				    setWebViewContent(result);
				}else{
					setWebContextDefault();
				}*/

				if (!"".equals(_temp_url) && null != _temp_url)
				{
					setWebViewContent(_temp_url);
				}
				else
				{
					setWebContextDefault();
				}
			}
		}.execute();
	}

	public void clickMessage(String url)
	{
		if (null == url || "".equals(url))
		{
			return;
		}
		new MessageClickAsyncTask(MessageContentActivity.this.getParent(),
				url)
		{
			String	_temp_url	= null;

			@Override
			protected Void doInBackground(String... params)
			{
				super.doInBackground(params);
				// TODO Auto-generated method stub
				_temp_url = this.getUrl();

				return null;
			}

			@Override
			protected void onPostExecute(Void result)
			{
				super.onPostExecute(result);
				/*if(!result.equals("")){
				    setWebViewContent(result);
				}else{
					setWebContextDefault();
				}*/

				if (!"".equals(_temp_url) && null != _temp_url)
				{
					setWebViewContent(_temp_url);
				}
				else
				{
					setWebContextDefault();
				}
			}
		}.execute();
	}

	@Override
	protected void onPause()
	{
		// TODO Auto-generated method stub
		super.onPause();
		ProgressDialog.dismissAll();
	}

	//setting webview content default
	public void setWebContextDefault()
	{
		TDStockLog.d(TAG, "setWebContextDefault");
		if (_webView != null)
		{
			String summary = "<html><head></head><body>"
					+ this.getString(R.string.service_not_available)
					+ "</body></html>";
			TDStockLog.d(TAG, "content=" + summary);
			_webView.loadDataWithBaseURL(null, summary, "text/html",
					"utf-8", null);
			isFinished = false;
		}
	}

	/* (non-Javadoc)
	 * @see cc.telecomdigital.tdstock.Framework.UniqueItem.NetworkChangeListener.INetworkChangeListener#NetworkChange_Response(cc.telecomdigital.tdstock.Framework.UniqueItem.NetworkChangeListener.CONNECT_TYPE, android.net.NetworkInfo)
	 */
	@Override
	public void NetworkChange_Response(CONNECT_TYPE iConnect,
			NetworkInfo iNetInfo)
	{
		if ((iNetInfo.isAvailable() || iNetInfo.isConnected()) && !isFinished)
		{
			postDelayed();
		}
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo)
	{
		copy.onCreateContextMenu(menu, v, menuInfo, COPY,
				getString(R.string.copy));
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		menu.add(0, COPY, 0, getString(R.string.copy));
		MenuItem mItem = menu.findItem(COPY);
		mItem.setIcon(R.drawable.copy32x32);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int c, MenuItem iItem)
	{
		TDStockLog.d(TAG, "onMenuItemSelected");
		super.onMenuItemSelected(c, iItem);
		if (iItem.getItemId() == COPY && _webView != null)
		{
			WebViewCopy.selectAndCopyText(_webView);
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		super.onOptionsItemSelected(item);
		TDStockLog.d(TAG, "onOptionsItemSelected");
		if (item.getItemId() == COPY && _webView != null)
		{
			//WebViewCopy.emulateShiftHeld(_webView);
			WebViewCopy.selectAndCopyText(_webView);
		}

		return true;
	}

	@Override
	public void onBackPressed()
	{
		_toolBtnReturn.performClick();
	}

}
